/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** You may use this file under the terms of the BSD license as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
**     of its contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtWidgets>
#include <QtOpenGL>

#include "glwidget.h"

GLWidget::GLWidget(QString objectModelPath, QWindow *parent)
    : QWindow(parent), m_objectModelPath(objectModelPath)
{
    clearColor = Qt::black;
    xRot = 0;
    yRot = 0;
    zRot = 0;
#ifdef QT_OPENGL_ES_2
    program = 0;
#endif
    QSurfaceFormat format;
    format.setVersion(3, 1);

    format.setRedBufferSize(8);
    format.setGreenBufferSize(8);
    format.setBlueBufferSize(8);
    format.setAlphaBufferSize(8);
    format.setDepthBufferSize(24);
    format.setSamples(4);

    format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);

    setSurfaceType(QWindow::OpenGLSurface);
    setFormat(format);

    create();

    startTimer(16*1);
// add another timer
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(rotateOneStep()));
    timer->start(150);

}

GLWidget::~GLWidget()
{
}

QSize GLWidget::minimumSizeHint() const
{
    return QSize(50, 50);
}

QSize GLWidget::sizeHint() const
{
    return QSize(200, 200);
}

void GLWidget::rotateBy(int xAngle, int yAngle, int zAngle)
{
    xRot += xAngle;
    yRot += yAngle;
    zRot += zAngle;
}

void GLWidget::setClearColor(const QColor &color)
{
    clearColor = color;
}


void GLWidget::setObjectModel(QString objectModelPath)
{
   // m_renderer = new SimpleRenderer(objectModelPath, this);
}

void GLWidget::initialize()
{
    m_renderer = new SimpleRenderer(m_objectModelPath, this);
    m_renderer->ensureInitialized();
 //   m_renderer->initialize();
}


void GLWidget::makeObject()
{

}
void GLWidget::timerEvent(QTimerEvent*)
{
    m_renderer->render();
}

void GLWidget::rotateOneStep()
{
    double zAngle = 0.0;
    double yAngle = 0.0;

    double angleStep = 5;
    yAngle = angleStep;

    if (m_rotationDir == DirLeft)
        yAngle = angleStep;
    if (m_rotationDir == DirRight)
        yAngle = -angleStep;
    if (m_rotationDir == DirUp)
        zAngle = angleStep;
    if (m_rotationDir == DirDown)
        zAngle = -angleStep;

    QMatrix4x4 rotation = m_renderer->rotation();
    rotation.rotate(zAngle, QVector3D(0, 0, 1));
    rotation.rotate(yAngle, QVector3D(0, 1, 0));
    m_renderer->setRotation(rotation);
}

void GLWidget::wheelEvent(QWheelEvent* event)
{
    double deltaX = event->angleDelta().x();
    double deltaY = event->angleDelta().y();

    qDebug() << "Window::wheelEvent(), " << "deltaX: " << deltaX << " deltaY: " << deltaY;

    double factor = 1.1;

    double zoomFactor = deltaY/120 * ((deltaY > 0)? factor: -1/factor);

    m_renderer->setScale(m_renderer->scale()*zoomFactor);
}

void GLWidget::keyPressEvent(QKeyEvent* event)
{
    double xDelta = 0;
    double yDelta = 0;

    double step = 0.1;

    if (event->key() == Qt::Key_Left) {
        xDelta = step;
        m_rotationDir = DirLeft;
    }
    if (event->key() == Qt::Key_Right) {
        xDelta = -step;
        m_rotationDir = DirRight;
    }
    if (event->key() == Qt::Key_Up) {
        yDelta = -step;
        m_rotationDir = DirUp;
    }
    if (event->key() == Qt::Key_Down) {
        yDelta = step;
        m_rotationDir = DirDown;
    }

    QVector2D translation = m_renderer->xyTranslation() + QVector2D(xDelta, yDelta);
    m_renderer->setXyTranslation(translation);

    double zAngle = 0.0;
    double yAngle = 0.0;

    double angleStep = 5;

    if (event->key() == Qt::Key_A)
        yAngle = angleStep;
    if (event->key() == Qt::Key_D)
        yAngle = -angleStep;
    if (event->key() == Qt::Key_W)
        zAngle = angleStep;
    if (event->key() == Qt::Key_S)
        zAngle = -angleStep;

    QMatrix4x4 rotation = m_renderer->rotation();
    rotation.rotate(zAngle, QVector3D(0, 0, 1));
    rotation.rotate(yAngle, QVector3D(0, 1, 0));
    m_renderer->setRotation(rotation);

}

